package com.twu.biblioteca;

import java.io.InputStream;
import java.util.*;

public class Biblioteca {

    private List<Menu> menus;
    private List<Book> books;
    private List<Movie> movies;
    private Map<String, String> loginInfo;
    private Map<String, User> users;
    private User currentUser = null;

    public Biblioteca() {
        initMenus();
        initBookList();
        initMovieList();
        initLoginInfo();
        initUserInfo();
    }

    private void initUserInfo() {
        users = new HashMap<String, User>();
        users.put("111-1111",new User("user1","email@email.com","0000"));
        users.put("111-1112",new User("user2","email@email.com","0000"));
    }

    private void initMenus() {
        menus = new ArrayList<Menu>();
        for(Menu menu : Menu.values()){
            menus.add(menu);
        }
    }

    private void initMovieList() {
        movies = new ArrayList<Movie>();
        movies.add(new Movie("Sholay",1975,"Ramesh Sippy",0));
        movies.add(new Movie("Despicable Me 2",2013,"Pierre Coffin/Chris Renaud",8.3));
        movies.add(new Movie("Léon",1994,"Luc Besson",9.4));
        movies.add(new Movie("Farewell My Concubine",1993,"Kaige Chen",9.4));
        movies.add(new Movie("3 Idiots",2009,"Rajkumar Hirani",9.1));
        movies.add(new Movie("The Wolverine",2013,"James Mangold",0));
        movies.add(new Movie("The King's Speech",2010,"Tom Hooper",8.2));
        movies.add(new Movie("Modern Times",1936,"Charles Chaplin",9.1));
        movies.add(new Movie("The Sixth Sense",1999,"M. Night Shyamalan",8.7));
        movies.add(new Movie("Titanic",1997,"James Cameron",8.9));
    }

    private void initBookList() {
        books = new ArrayList<Book>();
        books.add(new Book("Clean Code","Robert C. Martin",true));
        books.add(new Book("Thinking In Java","Bruce Eckel ",true));
        books.add(new Book("The HitchHiker's Guide to the Galaxy","Dogulas Adams",true));
        books.add(new Book("The Effective Executive","Peter F. Drucker",true));
        books.add(new Book("Asking the Right Question","M. Neil Browne/Stuart M. Keeley",true));
    }

    private void initLoginInfo() {
        loginInfo = new HashMap<String, String>();
        loginInfo.put("111-1111","123456");
        loginInfo.put("111-1112","123456");
    }

    public String greeting() {
        return "Welcome to Bangalore online Public Library";
    }

    public List<Menu> getMenus() {
        return menus;
    }

    public boolean checkSelection(List checkList, int selection) {
        return selection <= checkList.size() && selection > 0;
    }

    public String notifyInvalidSelection() {
        return "Select a valid option!!";
    }

    public List<Book> getBooks() {
        return books;
    }

    public String handleReserveSuccess() {
        return "Thank You! Enjoy the book.";
    }


    public String handleReserveFailure() {
        return "Sorry we don't have that book yet.";
    }

    public String displayMembershipDetails(boolean loginFlag) {
        if(loginFlag){
            return currentUser.toString();
        }
        return "Please talk to a Librarian. Thank you.";
    }

    public int getUserSelection(InputStream is) throws InputMismatchException{
        Scanner scanner = new Scanner(is);
        int selection;
        selection = scanner.nextInt();
        return selection;

    }

    public String presentMovie() {
        return "We have a new service online now! Please check out the movie list.";
    }

    public List<Movie> getMovies() {
        return movies;
    }

    public Map<String, User> getUsers() {
        return users;
    }

    public boolean verifyIfPasswordIsCorrect(String username, String password) {
        return loginInfo.get(username).equals(password);
    }

    public boolean checkIfUsernameExist(String username) {
        return loginInfo.containsKey(username);
    }

    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }

    public User getUserByUserName(String username) {
        return users.get(username);
    }

    public boolean verifyUserStatus() {
        return currentUser != null;
    }
}
